// pages/order/orderDetail/orderDetail.js
const app = getApp();

let api = require('../../../utils/api.js');

var QQMapWX = require('../../../public/js/qqmap-wx-jssdk.min.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailData: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    let that = this
   // console.log("options+++++++++++", options)

    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight - app.globalData.navHeight - 56
        });
      }
    });

    var token = wx.getStorageSync('TOKEN');
    var orderId = options.id;
    // var orderId = 348458871844864;
    var param = {
      id: orderId,
    };
    api._get('/omc/order/detail/' + orderId, param, token).then(res => {

      var detaildata = res.result;
      var shipmentTime = [];
      detaildata['showShipmentTime'] = {};
      shipmentTime['start'] = detaildata.shipmentTime.start * 1000;
      shipmentTime['end'] = detaildata.shipmentTime.end * 1000;
      detaildata['showShipmentTime'] = {
        start: app.formatDate(shipmentTime['start'], 'YYYY-MM-DD H'),
        end: app.formatDate(shipmentTime['end'], 'H')
      }


      var arrivalTime = [];
      detaildata['showArrivalTime'] = [];
      arrivalTime['start'] = detaildata.arrivalTime.start * 1000;
      arrivalTime['end'] = detaildata.arrivalTime.end * 1000;
      detaildata['showArrivalTime'] = {
        start: app.formatDate(arrivalTime['start'], 'YYYY-MM-DD H'),
        end: app.formatDate(arrivalTime['end'], 'H')
      }


      if (detaildata.freightWeight.max && detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.min + '-' + detaildata.freightWeight.max + '吨';
      } else if (detaildata.freightWeight.max && !detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.max + '吨';
      } else if (!detaildata.freightWeight.max && detaildata.freightWeight.min) {
        detaildata["showfreightWeight"] = detaildata.freightWeight.min + '吨';
      }


      if (detaildata.freightBulk.max && detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.min + '-' + detaildata.freightBulk.max + '方';
      } else if (detaildata.freightWeight.max && !detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.max + '方';
      } else if (!detaildata.freightBulk.max && detaildata.freightBulk.min) {
        detaildata["showfreightBulk"] = detaildata.freightBulk.min + '方';
      }



      if (detaildata.remarks) {
        detaildata['showRemarks'] = detaildata.remarks;
      } else {
        detaildata['showRemarks'] = '无';
      }
      detaildata['showSpecialAsk'] = '';
      if (detaildata.specialAsk.length != 0) {
        detaildata.specialAsk.forEach((v, i) => {
          if (i == 0) {
            detaildata['showSpecialAsk'] += v;
          } else {
            detaildata['showSpecialAsk'] += ',' + v;
          }
        })
      }
     // console.log(detaildata.owner)

      var createTime = detaildata['createTime'] * 1000;
      detaildata['showCreateTime'] = app.formatDate(createTime, 'YYYY-MM-DD');
      that.setData({
        detailData: detaildata
      });
     // console.log(res);
    })
  },

  
  /*
   *设置保证金
   */
  setDeposit: function(e) {

    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showCarModelStatus: true
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      });
    }.bind(this), 200)
  },

  //隐藏对话框
  hideModal: function() {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      keyboardHeight: 0
    })
    setTimeout(function() {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showCarModelStatus: false,
        keyboardHeight: 0
      })
    }.bind(this), 200)
  },
  // 获取键盘高度
  keyBoardHeight: function(e) {
    this.setData({
      keyboardHeight: e.detail.height
    });
  },
  createNext: function(e) {
    var that = this;
   // console.log(e)
    var orderId = e.currentTarget.dataset.orderid;
    var data = JSON.stringify(that.data.detailData);
    this.hideModal();
    wx.navigateTo({
      url: 'createNext?id=' + orderId + '&data=' + data,
    })
  },

  // 打电话
  callExpert: function(e) {
   // console.log(e.currentTarget.dataset.phone)
    var phone = e.currentTarget.dataset.phone;
    app.callExpert(phone);
  },
  
  showMap: function(e){
   // console.log(e.currentTarget.dataset.address)
    var qqmapsdk = new QQMapWX({
      key: 'NT2BZ-Y5Q34-YYCUF-DLQNZ-KW3L5-TCFCC' // 必填
    });
    var _this = this;
    //调用地址解析接口
    qqmapsdk.geocoder({
      //获取表单传入地址
      address: e.currentTarget.dataset.address, //地址参数，例：固定地址，address: '北京市海淀区彩和坊路海淀西大街74号'
      success: function(res) {//成功后的回调
       // console.log(res);
        var res = res.result;
        var latitude = res.location.lat;
        var longitude = res.location.lng;
        wx.getLocation({
          type: 'wgs84', 
          success: function (res) {
            wx.openLocation({//​使用微信内置地图查看位置。
              latitude: latitude,//要去的纬度-地址
              longitude: longitude,//要去的经度-地址
              name: e.currentTarget.dataset.address,
              address: e.currentTarget.dataset.address
            })
          }
        })
      }
    })

   
  }
})